import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import { isLogin } from '_utils'
Vue.use(VueRouter)
/* // 解决点击重定向的问题
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
} */

const routes = [
  {
    path: '/',
    redirect: '/Home'
  },
  {
    path: '/home',
    name: '首页',
    component: Home,
    meta: {
      needLogin: false,
      keepAlived: true
    }
  },
  {
    path: '/cate',
    name: '分类',
    component: () => import('_views/Cate'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/cart',
    name: '我的购物车',
    component: () => import('_views/Cart'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/favor',
    name: '我的收藏',
    component: () => import('_views/Favor'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/user',
    name: '个人中心',
    component: () => import('_views/User'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/detail',
    name: '商品详情',
    component: () => import('_views/Detail'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/listitem',
    name: '列表详情',
    component: () => import('_views/ListItem'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('_views/Login'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/register',
    name: '注册',
    component: () => import('_views/Register'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/search',
    name: '搜索',
    component: () => import('_views/Search'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/searchlist',
    name: '搜索结果',
    component: () => import('_views/SearchList'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/address',
    name: '联系人列表',
    component: () => import('_views/Address'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/addressAdd',
    name: '添加收货地址',
    component: () => import('_views/AddressAdd'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/addressUpdate',
    name: '修改联系人信息',
    component: () => import('_views/AddressUpdate'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/order',
    name: '我的订单',
    component: () => import('_views/Order'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/orderContact',
    name: '收货人列表',
    component: () => import('_views/OrderContact'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/edit',
    name: '信息修改',
    component: () => import('_views/Edituser'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '*',
    name: '找不到页面',
    component: () => import('_views/Notfound'),
    meta: {
      needLogin: false
    }
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由前置守卫登录鉴权
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    // 需要登录
    if (isLogin()) {
      // 登录了
      next()
    } else {
      // 未登录
      next({
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    // 不需要登录
    next()
  }
})

export default router
